package yunquan.wang.modules.base.web;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import yunquan.wang.base.service.BaseService;
import yunquan.wang.base.web.BaseController;
import yunquan.wang.base.web.WebConstants;
import yunquan.wang.base.web.WebHelper;
import yunquan.wang.modules.base.entity.CodeType;
import yunquan.wang.modules.base.entity.CodeValue;
import yunquan.wang.modules.base.service.AppCodeService;

@Controller
@RequestMapping("/core/appcode")
public class AppcodeController extends BaseController<CodeType>{

@Resource
private	AppCodeService appcodeService;

	@RequestMapping(value = "/list.do")
	public ModelAndView list() {
		return baseList();
	}

 
	@RequestMapping(value = "/save.do")
	public ModelAndView save(@Valid CodeType entity,
			BindingResult bindingResult,RedirectAttributes redirectAttributes) {
		 
		ModelAndView mv= new ModelAndView();
		redirectAttributes.addAttribute("test", "TestString");
		entity=getService().save(entity);
		mv.addObject("typeId", entity.getId());
		mv.setViewName(WebHelper.getControllerPath()+"/codelist");
		return mv;
		
	}

 
	@RequestMapping(value = "/view.do")
	public ModelAndView view(String id) {
 
		return baseView();
	}

	@RequestMapping(value = "/input.do")
	public ModelAndView input(String id) {
		ModelAndView mv= new ModelAndView();
	   CodeType	entity;
		if(StringUtils.isNotBlank(id))
		{
			entity=appcodeService.get(id);
			mv.addObject("listCode", appcodeService.getCodeValues(id));
		}
		else
		{
			entity=new CodeType();
		}
		mv.addObject(WebConstants.ATTRIBUTE_ENTITY, entity);
		return mv;
	}
	@RequestMapping(value = "/codeinput.do")
	public ModelAndView editCode(String codeTypeId,String id)
	{
		ModelAndView mv=new ModelAndView(WebHelper.getControllerPath()+"/codeinput");
		CodeValue entity;
		if(StringUtils.isNotBlank(id))
		{
			entity=appcodeService.getCodeValue(id);
		}
		else
		{
			entity=new CodeValue();
			CodeType codetype=new CodeType();
			codetype.setId(codeTypeId);
			entity.setCodeType(codetype);
			//TODO 肯定是前面传了值
			
		}
		mv.addObject("typeId", codeTypeId);
		mv.addObject(WebConstants.ATTRIBUTE_ENTITY, entity);
        return mv;
		
	}
	
	@RequestMapping(value = "/codesave.do")
	public ModelAndView saveCode(CodeValue entity)
	{
		CodeType type=appcodeService.get(entity.getCodeType().getId());
		entity.setCodeType(type);
		ModelAndView mv=new ModelAndView(WebHelper.getControllerPath()+"/codelist");
 
		entity=appcodeService.saveCodeValue(entity);
		 List<CodeValue> listCode=	appcodeService.getCodeValues(entity.getCodeType().getId());
		mv.addObject("listCode",listCode );
		mv.addObject("typeId", entity.getCodeType().getId());
        return mv;
	}
	@RequestMapping(value = "/codelist.do")
	public ModelAndView codeList(String typeId)
	{
		ModelAndView mv=new ModelAndView(WebHelper.getControllerPath()+"/codelist");
		 List<CodeValue> listCode=	appcodeService.getCodeValues(typeId);
		mv.addObject("listCode",listCode );
		mv.addObject("typeId", typeId);
		return mv;
	}
	@RequestMapping(value = "/codedelete.do")
	public ModelAndView saveCode(String id,String codeTypeId)
	{
		ModelAndView mv=new ModelAndView(WebHelper.getControllerPath()+"/codelist");
		appcodeService.delete(id);
		mv.addObject("listCode", appcodeService.getCodeValues(codeTypeId));
        return mv;
	}
	
	@RequestMapping(value = "/delete.do")
	public ModelAndView delete(String id) {
		return baseDelete();
	}


	@Override
	public BaseService<CodeType> getService() {
		return appcodeService;
	}
}
